import scrapy

from ScrapyObject.spiders.utils.url_utils import *

'''
scrapy crawl ltae -o ltae.json
https://66sex.tv/
'''


class LtaeSpider(scrapy.Spider):
    name = "ltae"
    # 前缀
    prefix = 'https://'
    # 中缀
    website = '66sex'
    # 后缀
    suffix = '.tv/'
    allowed_domains = [website + '.tv']
    start_urls = [prefix + website + suffix]

    def __init__(self):
        self.i = 0

    def parse(self, response):
        content = get_data(response)
        video_url = get_video_url_one(content)
        tags = response.xpath("//h2[@class='text-nowrap']//a/text()").extract()
        if len(video_url) and len(tags):
            self.i = self.i + 1
            yield get_video_item(id=self.i, tags=tags[0], url=response.url, name=tags[-1], pUrl='', vUrl=format_url_one(video_url[0]))
        else:
            picture_url = response.xpath("//ul[@class='thumbnail-group clearfix']//li//a//img/@ data-original").extract()
            urls = response.xpath("//ul[@class='thumbnail-group clearfix']//li//h5//a/@ href").extract()
            if len(picture_url) and len(urls):
                for i in range(len(urls)):
                    self.i = self.i + 1
                    yield get_video_item(id=self.i, tags="", url=split_joint(self.prefix + self.website + self.suffix, urls[i]), name="", pUrl=picture_url[i], vUrl='')
        url_list = get_url(content)
        # 提取url
        for url in url_list:
            if url.startswith('/') and url.endswith('.html'):
                yield scrapy.Request(split_joint(self.prefix + self.website + self.suffix, url), callback=self.parse)
